package sealte.hvpms.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import sealte.hvpms.dao.AdminDAO;
import sealte.hvpms.dao.UserDAO;
import sealte.hvpms.dao.impl.AdminDAOImpl;
import sealte.hvpms.dao.impl.UserDAOImpl;
import sealte.hvpms.pojo.Admin;
import sealte.hvpms.pojo.UserCreate;

public class Login extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html");
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		String utype = request.getParameter("utype");
		String name = request.getParameter("username");
		String password = request.getParameter("userpassword");
		String message = "";
		if(utype.equals("u")){
			UserDAO ub = new UserDAOImpl();
			message = ub.login(name, password);
			if(message.equals("OK")){
				int uc_id = ub.getID(name, password);
				UserCreate uc = new UserCreate();
				uc.setId(uc_id);
				uc.setName(name);
				HttpSession session = request.getSession();
				session.setAttribute("UserCreate", uc);
				request.getRequestDispatcher("home/users_create.jsp").forward(request, response);				
			}else{
				request.setAttribute("message", message);
				request.getRequestDispatcher("index.jsp").forward(request, response);
			}
		}else if(utype.equals("a")){
			AdminDAO ab = new AdminDAOImpl();
			message = ab.login(name, password);
			if(message.equals("OK")){
				Admin admin = ab.getAdminAll(name, password);
				HttpSession session = request.getSession();
				session.setAttribute("admin", admin);
				request.getRequestDispatcher("hvpms_admin/admin.jsp").forward(request, response);
			}else{
				request.setAttribute("message", message);
				request.getRequestDispatcher("hvpms_admin/index.jsp").forward(request, response);
			}
		}
		
	}

}
